<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="showAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button
                v-if="row.type === 1"
                :disabled="row.type === 2"
                size="small"
                type="text"
                @click="showAdd(2, row.id)"
              >
                添加权限点
              </el-button>
              <el-button size="small" type="text" @click="lock(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible.sync="showDialog" width="40%" :title="showTitle" @close="close">
      <!-- 表单内容 -->
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" placeholder="请输入权限的描述" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="clickSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  delPermission,
  getPermissionDetail,
  updatePermission
} from '@/api/permission'
import { listToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      form: {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空' }],
        code: [{ required: true, message: '权限标识不能为空' }]
      }
    }
  },
  computed: {
    showTitle() {
      if (this.form.id) {
        return '查看权限'
      } else {
        return '添加权限'
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const data = await getPermissionList()
      this.list = listToTree(data, '0')
      // console.log(this.list)
    },
    showAdd(type, pid) {
      // type: 1 访问权控制, 2 操作权控制
      // pid: 标识当前数据的父节点
      this.showDialog = true
      this.form.type = type
      this.form.pid = pid
      // console.log(type, pid)
    },
    async clickSubmit() {
      try {
        this.$refs.form.validate()
      } catch {
        return
      }
      if (this.form.id) {
        await updatePermission(this.form)
      } else {
        await addPermission(this.form)
      }
      this.$message.success('操作成功')
      this.getPermissionList()
      this.showDialog = false
    },
    async del(id) {
      try {
        await this.$confirm('你确定要删除该权限吗？', '温馨提示', { type: 'warning' })
      } catch {
        return
      }
      // 发送请求删除该数据
      await delPermission(id)
      this.getPermissionList()
      this.$message.success('删除成功')
    },
    async lock(id) {
      const data = await getPermissionDetail(id)
      this.form = data
      this.showDialog = true
    },
    close() {
      this.showDialog = false
      this.form = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  }
}
</script>
